package com.erp.controller;

import com.erp.mapper.SupplierMapper;
import com.erp.tool.EmailTool;
import com.erp.tool.PgTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SupplierController {

    @Autowired
     private SupplierMapper mapper;

    @Autowired
    private EmailTool emailTool;

    @RequestMapping("/planEmail")
    public Map test(MultipartFile excelFile, String to){
        Map<String,String>map=new HashMap<String,String>();
        String text = "我们公司需要以下物品:";
        try {
            String s = emailTool.emailGive("采购计划通知信：", text, to, excelFile.getInputStream());
            map.put("msg",s);
        } catch (MessagingException | IOException e) {
            e.printStackTrace();
        }
        return map;
    }


    @RequestMapping("/limitSupplier")
    public List limitUsers(String name, int num){
        List<Object>allList = new ArrayList<Object>();
        int row=4;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("name",name);
        map.put("end",row);
        map.put("start", PgTool.getStart(num,row).get("start"));
        System.out.println(map);
        //查询需多少页
        allList.add(PgTool.getPage(row, mapper.countSupplier(map)));
        //得到分页数据
        allList.add(mapper.findAllSupplier(map));
        return allList;
    }
}
